/*
 * Copyright 2007-2008, www.jsemantic.org, www.adolfoestevez.com,
 * http://semanticj2ee.blogspot.com/ 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jservicerules.support.flex;

import org.jservicerules.support.semanticservice.SemanticServiceFactory;

import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;

/**
 * <p>
 * Support class that integrates the jSemanticService with the Flex Remoting
 * Data Services (Blaze DS) framework. This class makes available instances of
 * the Semantic Services stored in a Spring context to the Blaze DS context, so
 * they can be exposed as Remote Services.<br>
 * <p>
 * This factory is configured in the services-config.xml file (Blaze DS).<br>
 * 
 * <code>
 *    
 * <factories>
 *   <factory id="semanticEvenServiceFactory" class="org.jservicerules.support.flex.FlexSemanticServiceFactory">
 *			<properties>
 *				<prop1>evenService</prop1>
 *       		<prop2>org/jsemantic/support/examples/even/services.xml</prop2>
 *      	</properties>
 *	 </factory>
 * </factories>
 * 
 * <code>
 * <br>
 * <p>
 * Two parameters must be passed to the factory:<br>
 * <ul>
 * <li>prop1: the id of the service (in the spring context) that is going to be made available to Blaze DS.</li>
 * <li>prop2: the location in the classpath of the spring context configuration file.</li>
 * </ul>
 * <br>
 * 
 * <p>
 * Now the even service can be exposed as a Remote Service in the remoting-config.xml file (Blaze DS): <br>
 * 
 * <code>
 * 
 * <destination id="evenService">
 *	  <properties>
 *          <factory>semanticEvenServiceFactory</factory>
 *          <source>org.jservicerules.support.semanticservice.StatelessSemanticService</source>
 *         <scope>application</scope>
 *    </properties>
 *</destination>
 * 
 * </code>
 *<br>
 */
public class FlexSemanticServiceFactory implements FlexFactory {

	/** The SERVIC e_ id. */
	public static String SERVICE_ID = "prop1";

	/** The CONTEX t_ file. */
	public static String CONTEXT_FILE = "prop2";

	/** The service factory. */
	private SemanticServiceFactory serviceFactory = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see flex.messaging.FlexFactory#createFactoryInstance(java.lang.String,
	 *      flex.messaging.config.ConfigMap)
	 */
	public FactoryInstance createFactoryInstance(String arg0, ConfigMap arg1) {
		return new FactoryInstance(this, arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see flex.messaging.FlexFactory#lookup(flex.messaging.FactoryInstance)
	 */
	public Object lookup(FactoryInstance arg0) {
		return serviceFactory.getInstance();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see flex.messaging.FlexConfigurable#initialize(java.lang.String,
	 *      flex.messaging.config.ConfigMap)
	 */
	public void initialize(String arg0, ConfigMap arg1) {
		String id = arg1.getProperty(SERVICE_ID);
		String configFile = arg1.getProperty(CONTEXT_FILE);
		serviceFactory = new SemanticServiceFactory(id, configFile);
	}

}
